package ln.javatest.day01.demo03;

import ln.javatest.day01.demo02.JDBCUtils;
import org.junit.Test;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class JDBCTemplateDemo02 {
    //Junit单元测试，可以让方法独立的执行，不依赖于主方法
    //1.修改2号的成绩为78

    //获取JDBCTemplate对象
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Test
    public void test1(){
        //2.定义sql
        String sql = "update student set sorce = 78 where id = 2";
        //3.执行sql
        int count = template.update(sql);
        System.out.println(count);
    }
    //2.添加一条记录
    @Test
    public void test2(){
        //2.定义sql
        String sql = "insert into student(id,namee,sorce) values(?,?,?)";
        //3.执行sql
        int count = template.update(sql,4,"赵六",88);
        System.out.println(count);
    }
    //3.删除刚才添加的记录
    @Test
    public void test3(){
        //2.定义sql
        String sql = "delete from student where id = ?";
        //3.执行sql
        int count = template.update(sql,4);
        System.out.println(count);
    }
    //4.查询id为3的记录，将其封装为Map集合
    @Test
    public void test4(){
        String sql = "select * from student where id = ?";
        //注意查询的结果集长度只能为1
        //将查询的字段名作为key，值作为value，将这条记录封装为map集合
        // 错误 String sql = "select * from student where id = ? or id = ?";
        // 错误 Map<String, Object> map = template.queryForMap(sql, 3,2);
        Map<String, Object> map = template.queryForMap(sql, 3);
        System.out.println(map);
        //{id=3, namee=易烊千玺, sorce=89, sex=nan, age=23}
    }
    //5.查询所有记录，将其封装为List集合
    @Test
    public void test5(){
        String sql = "select * from student";
        //每一条记录封装为一个Map集合，再将Map集合装载到List集合中
        List<Map<String, Object>> list = template.queryForList(sql);
        for (Map<String, Object> stringObjectMap : list) {
            System.out.println(stringObjectMap);
        }
    }
    //6.查询所有记录，将其封装为Student对象的List集合
    @Test
    public void test6(){
        String sql = "select * from student";
        List<Student> list = template.query(sql, new RowMapper<Student>() {
            @Override
            public Student mapRow(ResultSet rs, int i) throws SQLException {
               Student student= new Student();
               int id = rs.getInt("id");
               String namee = rs.getString("namee");
               int sorce = rs.getInt("sorce");
               String sex = rs.getString("sex");
               int age = rs.getInt("age");

               student.setId(id);
               student.setNamee(namee);
               student.setSorce(sorce);
               student.setSex(sex);
               student.setAge(age);
               return student;
            }
        });
        for (Student student : list) {
            System.out.println(student);
        }
    }

    //6.查询所有记录，将其封装为Student对象的List集合
    //BeanPropertyRowMapper：这个要求类中的数据得是引用类型数据 int-->Integer
    @Test
    public void test6_2(){
        String sql = "select * from student";
        List<Student> list = template.query(sql, new BeanPropertyRowMapper<Student>(Student.class));
        for (Student student : list) {
            System.out.println(student);
        }
    }
    //7.查询总记录数
    @Test
    public void test7(){
        String sql = "select count(id) from student";
        Long total = template.queryForObject(sql, Long.class);//count（id）返回的是long类型的参数
        System.out.println(total);
    }
}
